package com.yqg.protection.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yqg.protection.controller.dto.ImportFireLawsDto;
import com.yqg.protection.entity.FireLaws;
import com.yqg.protection.listenern.FireLawsListener;
import com.yqg.protection.mapper.FireLawsMapper;
import com.yqg.protection.service.IFireLawsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * FireLawsServiceImpl 服务实现类
 * @author zxf
 * @since 2024-09-11
 */
@Service
@Slf4j
public class FireLawsServiceImpl extends ServiceImpl<FireLawsMapper, FireLaws> implements IFireLawsService {

    @Autowired
    FireLawsMapper fireLawsMapper;

    @Autowired
    FireLawsListener fireLawsListener;

    @Override
    public void export(HttpServletResponse response, String keyWord, String enactStartTime, String enactEndTime, String materialStartTime, String materialEndTime) {
        try {
            //设置下载
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = null;
            fileName = URLEncoder.encode("消防法规信息表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

            //查询用户所有数据
            QueryWrapper<FireLaws> queryWrapper = new QueryWrapper<>();
            if (enactStartTime!=null && !enactStartTime.equals("")) {
                queryWrapper.between("enactment_date", enactStartTime, enactEndTime);
            }
            if (materialStartTime!=null && !materialStartTime.equals("")) {
                queryWrapper.between("material_date", materialStartTime, materialEndTime);
            }
            queryWrapper.and(qw -> qw.like("laws_name", keyWord).or().like("promulgat_author",keyWord).or().like("promulgat_number",keyWord));
            queryWrapper.eq("is_delete", 0);
            queryWrapper.orderByDesc("id");
            List<FireLaws> exportData = fireLawsMapper.selectList(queryWrapper);
            //EasyExcel 写操作
            EasyExcel.write(response.getOutputStream(), FireLaws.class)
                    .sheet("消防法规信息表")
                    .doWrite(exportData);
        } catch (Exception e) {
            log.info("exception ", e);
        }
    }

    @Override
    public void importData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), ImportFireLawsDto.class, fireLawsListener)
                    .sheet(0).headRowNumber(1).doRead();
        } catch (Exception e) {
            log.info("导入异常，请稍后重试！", e);
        }
    }
}
